﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DCT.Huiteng.Utility.Log;
using System.IO;
using DCT.Huiteng.Utility.Config;

namespace DCT.Huiteng.AdminSystem.Web.Codes
{
    public class Logger
    {
        private static readonly object lockobj = new object();
        private static ILog instance = null;

        public static ILog Instance
        {
            get
            {
                if (instance == null)
                {
                    throw new NullReferenceException("Logger.Instance is null; Please invoke Logger.Initial");
                }
                return instance;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmllogConfig">在~/configs/[].config的日志配置文件名称</param>
        /// <returns></returns>
        public static ILog Initial(string xmllogConfig)
        {
            return Initial(GeneralToolCode.LoadConfig(xmllogConfig));
        }

        public static ILog Initial(IConfigParser cp)
        {
            if (cp == null) {
                throw new NullReferenceException("cp is null");
            }
            lock (lockobj)
            {
                instance = new FileLog();
                instance.LoadConfig(cp);
            }
            return Logger.Instance;
        }

        public static ILog Initial(ILog log)
        {
            if (log == null)
            {
                throw new NullReferenceException("log is null");
            }
            lock (lockobj)
            {
                instance = log;
            }
            return Logger.Instance;
        }
    }
}